<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.longtop.com/intelliweb" prefix="e"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>机构列表-SJH</title>
</head>

<e:View id="view">
   <script type="text/javascript">SkinUtil.getSkin();</script>
   <script type="text/javascript">SkinUtil.getModuleSkin();</script>
  
<div id="Invisual Controls" style="min-height: 40px; background-color: LemonChiffon;" >
	<e:Dataset id="Dataset_Org" datasetDefinition="DatasetDefinition_sys.dsd" datasetRef="Dataset_Org" 
		pageSize="-1" loadDataCommand="Command_Load" onAfterScroll="Dataset_Org_onAfterScroll(dataset)">
		<e:Field name="orgId" dataType="string" label="机构编号" maxLength="32" />
		<e:Field name="orgName" dataType="string" label="机构名称" maxLength="200" />
		<e:Field name="parentOrgId" dataType="string" label="上级机构编号" maxLength="32" />
		<e:Field name="orgLevel" dataType="integer" label="机构级次" maxLength="1" />
		<e:Field name="orgLevelCode" dataType="string" label="机构级次码" maxLength="100" />
		<e:Field name="sortNo" dataType="integer" label="同级机构顺序号" maxLength="4" />
		<e:Field name="remark" dataType="string" label="备注" maxLength="200" />
	</e:Dataset>
	
	<e:Command id="Command_Save" action="com.longtop.framework.sys.org.web.OrgListAction" 
		method="saveOrg">
		<e:DatasetInfo name="info1" dataset="Dataset_Org"></e:DatasetInfo>
	</e:Command>
	<e:Command id="Command_Load" action="com.longtop.framework.sys.org.web.OrgListAction" 
		method="loadOrg" showHintInfoOnSuccess="false">
		<e:DatasetInfo name="info1" dataset="Dataset_Org" alwayRefreshClient="true" submitType="none"></e:DatasetInfo>
	</e:Command>
	<e:Command id="Command_HasChild" action="com.longtop.framework.sys.org.web.OrgListAction" 
		method="hasChild" showHintInfoOnSuccess="false">
		<e:Parameter name="id"></e:Parameter>
	</e:Command>
</div>

<body>
	<e:Page id="ePage" action="com.longtop.framework.sys.org.web.OrgListAction" style="min-height: 80px; width: 100%; height: 100%;" onAfterLoadPage="ePage_onAfterLoadPage()">
		<table style="width: 100%; height: 100%;" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
			<tr>
				<td align="left" valign="top">
					<e:Panel id="Panel_Search" style="width: 285px; height: 100%" caption="机构树" captionIcon="/images/query_win.gif" captionStyle="color:#888888" allowFold="true" showArrowButton="false">
						<table style="text-align: left; width: 100%;" border="0" cellpadding="3" cellspacing="3" width="100%" align="center">
							<tr>
								<td align="center">
									<e:Button id="Button_Add" caption="新增" style="height: 22px; width: 70px" icon="/images/add.gif" 
										onClick="Button_Add_onClick()"></e:Button>&nbsp;&nbsp;
									<e:Button id="Button_Delete" caption="删除" style="height: 22px; width: 70px" icon="/images/delete.gif" 
										onClick="Button_Delete_onClick()"></e:Button>&nbsp;&nbsp;
									<e:Button id="Button_Save" caption="保存" style="width: 70px; height: 22px" icon="/images/submit.gif" 
										onClick="Button_Save_onClick()"></e:Button>
								</td>
							</tr>
							<tr>
								<td align="left">
									<e:DataTree id="DataTree_Org" dataset="Dataset_Org" nameField="orgId" valueField="orgId"
										parentField="parentOrgId" labelField="orgName" 
										style="border-color:#3ABCFA;width: 100%; height: 390; text-align: left"
										showRoot="false" rootLabel="机构树" lazyLoading="true" 
										useDefaultFolderIcon="true" contextMenuItems="expand,collapse">
									</e:DataTree>
								</td>
							</tr>
						</table>
					</e:Panel>
				</td>
				<td valign="top" width="100%">
					<e:Panel id="Panel_Result" style="width: 100%; height: 100%" captionStyle="color:#888888" caption="机构明细信息" captionIcon="/images/list_win.gif" showArrowButton="false">
						<table style="text-align: left; width: 100%;" border="0" cellpadding="3" cellspacing="3" >
							<tr>
								<td width="130px" align="right">机构编号</td>
								<td>
									<e:Text id="id" dataset="Dataset_Org" field="orgId" style="width: 350px"></e:Text>
									<font color="red">*</font>
								</td>
							</tr>
							<tr>
								<td width="130px" align="right">机构名称</td>
								<td>
									<e:Text id="name" dataset="Dataset_Org" field="orgName" style="width: 350px"></e:Text>
									<font color="red">*</font>
								</td>
							</tr>
							<tr>
								<td align="right">上级机构编号</td>
								<td>
									<e:Text id="parentId" dataset="Dataset_Org" field="parentOrgId" style="width: 350px" readOnly="true"></e:Text>
								</td>
							</tr>
							<tr>
								<td align="right">机构级次</td>
								<td>
									<e:Text id="orgLevel" dataset="Dataset_Org" field="orgLevel" style="width: 350px" readOnly="true"></e:Text>
								</td>
							</tr>
							<tr>
								<td align="right">机构级次码</td>
								<td>
									<e:Text id="orgLevelCode" dataset="Dataset_Org" field="orgLevelCode" style="width: 350px" readOnly="true"></e:Text>
								</td>
							</tr>
							<tr>
								<td align="right">同级机构顺序号</td>
								<td>
									<e:Text id="sortNo" dataset="Dataset_Org" field="sortNo" style="width: 350px" dataType="integer"></e:Text>
									<font color="red">*</font>
								</td>
							</tr>
							<tr>
								<td align="right">备注</td>
								<td>
									<e:Text id="remark" dataset="Dataset_Org" field="remark" style="width: 350px"></e:Text>
								</td>
							</tr>
						</table>
					</e:Panel>
				</td>
			</tr>
	    </table>
	</e:Page>
</body>
</e:View>
</html>	

<script type="text/javascript">
//init
function ePage_onAfterLoadPage(){
	var orgDataTree = $J("DataTree_Org");
    if (!orgDataTree) {
        MsgBox.showWarnMsg("机构树还未生成！");
        return;
    }
    if (!orgDataTree.currentNode) {
    	orgDataTree.setCurrentNode();
	}
    orgDataTree.expandNode();//展开当前节点
}
	
//触发查看当前选中的机构明细，设定readonly属性	
function Dataset_Org_onAfterScroll(dataset){
	var crecord = dataset.currentRecord;
	if(crecord != null){
		var state = crecord.state;
		if (state == Constant.RECORD.STATE_INSERT || state == Constant.RECORD.STATE_NEW) {
			parentId_Syn(crecord);	//新增记录，自动关联上级菜单编号：
			var field = dataset.getField("orgId");
			field.setReadOnly(false);
		}else{
			var field = dataset.getField("orgId");
			field.setReadOnly(true);
		}	
	}
}
//新增机构
function Button_Add_onClick(){
/*
	//是否需要先保存
	var record = Dataset_Org.getFirstRecord();
	var flag = false; 	
	while(record){
		if(record.state != 0){
			flag = true;
			break;
		}		
		record =  record.getNextRecord();
	}
	
	if(flag){
		alert("你还有未保存的操作，请先保存！");
		return;
	}
*/
	var orgDataTree = $J(DataTree_Org);
    //如果树对象不存在，则返回
    if (!orgDataTree) 
    	return;
    orgDataTree.insertChildNode();
}

//删除
function Button_Delete_onClick(){
	var tree = $J(DataTree_Org);
	
	//如果树对象不存在，则返回
    if (!tree) {
        MsgBox.showWarnMsg("菜单树还未生成！");
        return;
    }
	if (!tree.currentNode) {
		MsgBox.showWarnMsg("请选择结点！");
		return;
	}
	if (tree.currentNode.isVirtualRoot) {
		MsgBox.showWarnMsg("不能删除根结点！");
		return;
	}	
		
	//先从客户端树结点集对象中，判断当前结点是否有下级结点。//只支持不延迟加载
    if (tree.currentNode.childNodes && tree.currentNode.childNodes.length > 0) {
        MsgBox.showWarnMsg("当前菜单有下级菜单，不能删除！","");
        return;
    }

    //去服务器验证	【注意】
	var orgId = tree.currentNode.getName();//tree设置了nameFiled属性。对应了id。
	Command_HasChild.parameters.setValue("id",orgId);
	Command_HasChild.execute();
	
	var hasChild=Command_HasChild.parameters.getParameter("hasChild").value;
	if(hasChild == "true"){
		MsgBox.showWarnMsg("当前菜单有下级菜单，不能删除。","");
		return;
	}
	//删除节点:
	MsgBox.showConfirmMsg("您确定要删除菜单[" + tree.currentNode.getLabel() + "]吗？","",[doDelete,tree]);
}
//确定删除
function doDelete(tree){
	var dataset = $J(Dataset_Org);
    var state = dataset.currentRecord.state;
    tree.deleteNode();//先客户端删除记录
    //如果不是本次新增的，则再去服务器上删数据库记录。
    if (state != "1" &&  state != "4") {
        Command_Save.execute();
   	}
}

//保存
function Button_Save_onClick(){
	//必输项校验
	var record = Dataset_Org.getFirstRecord();
	while(record){
		if(record.state != 0){//正常状态的跳过
			var id = record.getValue("orgId").Trim();
			if(isEmpty(id)){
				MsgBox.showWarnMsg('机构编号不能为空。');
				return false;
			}
			var name = record.getValue("orgName").Trim();
			if(isEmpty(name)){
				MsgBox.showWarnMsg('机构编号[ ' + id + ' ]的机构名称不能为空。');
				return false;
			}
			var sortNo = record.getValue("sortNo")+"";
			if(isEmpty(sortNo)){
				MsgBox.showWarnMsg('机构编号[ ' + id + ' ]的机构排序号不能为空。');
				return false;
			}
			//菜单编号不能和上级菜单编号一样
			var parentId = record.getValue("parentOrgId");
			if(id == parentId){
				MsgBox.showWarnMsg('机构编号[ ' + id + ' ]不能和上级机构编号一样。');
				return false;
			}
			if(record.state == 1 || record.state == 4){
				parentId_Syn(record);	//自动关联上级菜单编号：
			}
		}
		record =  record.getNextRecord();
	}
	var flag = Command_Save.execute();
	if(flag){
		Dataset_Org.getField("orgId").setReadOnly(true);//保存成功后，ID字段只读。
	}
}
//自动关联上级菜单编号：
function parentId_Syn(crecord){
	var tree = $J("DataTree_Org");
	var parentRecord = tree.getNodeByRecord(crecord).parentNode.record;
	if(parentRecord){
		//alert(parentRecord.getValue("id"));
		crecord.setValue("parentOrgId",parentRecord.getValue("orgId"));
	}
}	
</script>